/* getimglinks_mintmanga.c
 *
 * Getting image links from chapter page for MintManga.com
 *
 * In MintManga, links are divided by parts.
 * So we should concatenate these parts for getting the full link.
 * Number of link parts is setted in 'LINKS_PARTS_MAX' constant.
 * */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "getimglinks.h"

#define LINK_PARTS_MAX 3
#define LP_BUF 1

/*
 * Mintmanga uses quotes for parts of image links.
 * Every part of image link has own type of quote: single or double.
 */
static char lp_quote[3] = {
	'\'',
	'\'',
	'\"'
};

struct imglinks *
getimglinks_mintmanga(struct input_data *id)
{
	/* iteration variables declaration */
	int i;

	/* normal variables declaration */
	struct imglinks *ils;

	/* iteration variables declaration and initialization */
	int char_num  = 0;    /* total number of processed chars */
	int lp_num    = 0;    /* link's part number */
	int lp_ch_num = 0;    /* lp's char number */

	/* bool variables declaration and initialization */
	char is_getting_lp = 0;

	char *links_line = strstr(id->data, "rm_h.init");
	if (links_line == NULL) {
		fprintf(stderr,
						"getimglinks_readmanga.c: error getting strstr(..., \"rm_h.init\")\n");
		exit(EXIT_FAILURE);
	} 

	ils = (struct imglinks *) malloc(sizeof(struct imglinks));
	if (ils == NULL) {
		fprintf(stderr,
						"getimglinks_readmanga.c: error allocating ils.\n");
	}
	ils->num = 0; /* links number */

	char **lps_arr = malloc(LINK_PARTS_MAX * sizeof(char *));
	if (lps_arr == NULL) {
		fprintf(stderr,
		        "getimglinks_readmanga.c: error allocating lnk_parts_arr.\n");
		exit(EXIT_FAILURE);
	}

	while (links_line[char_num] != ';') {
		/* generate link part*/
		if (is_getting_lp) {
			/* check if lps_arr is freed */
			if (lps_arr == NULL) {
				lps_arr = malloc(LINK_PARTS_MAX * sizeof(char *));
			}

			lps_arr[lp_num] = malloc(LP_BUF * sizeof(char));
			if (lps_arr[lp_num] == NULL) {
				fprintf(stderr,
				        "getimglinks_readmanga.c: error allocating lps_arr[%d]",
				        lp_num);
				exit(EXIT_FAILURE);
			}

			while (links_line[char_num] != lp_quote[lp_num]) {
				/* memory allocation for lps_arr[lp_num] */
				if (lp_ch_num >= LP_BUF) {
					lps_arr[lp_num] = realloc(lps_arr[lp_num],
					                          (lp_ch_num + 1) * sizeof(char));
					if (lps_arr[lp_num] == NULL) {
						fprintf(stderr,
						        "getimglinks_readmanga.c:77: error reallocating lps_arr[%d]",
						        lp_num);
						exit(EXIT_FAILURE);
					}
				} else {
					lps_arr[lp_num] = malloc((LP_BUF +1) * sizeof(char));
					if (lps_arr[lp_num] == NULL) {
						fprintf(stderr,
						        "getimglinks_readmanga.c:77: error allocating lps_arr[%d]",
						        lp_num);
						exit(EXIT_FAILURE);
					}
				}
				/* ignore gibberish parts in link parts */
				if (links_line[char_num] == '?') {
					break;
				}
				/* link part generation */
				lps_arr[lp_num][lp_ch_num] = links_line[char_num];
				lp_ch_num++;
				char_num++;
			}
			lps_arr[lp_num][lp_ch_num] = '\0';
			lp_ch_num = 0;
			lp_num++;
			is_getting_lp = 0;

			} else if (links_line[char_num] == lp_quote[lp_num]) {
				is_getting_lp = 1;
			}

			/* if lps are generated, generate imglink */
			if (lp_num == LINK_PARTS_MAX) {
				/* memory allocation for ils->lst part */
				if (ils->lst == NULL) {
					ils->lst = malloc(sizeof(struct imglink *));
					if (ils->lst == NULL) {
						fprintf(stderr,
						        "getimglinks_readmanga.c: error allocating ils->lst");
						exit(EXIT_FAILURE);
					}
				} else {
					ils->lst = realloc(ils->lst, (ils->num + 1) * 
					                             sizeof(struct imglink *));
					if (ils->lst == NULL) {
						fprintf(stderr,
						        "getimglinks_readmanga.c: error reallocating ils->lst");
						exit(EXIT_FAILURE);
					}
				}
				
				ils->lst[ils->num] = (struct imglink *) malloc(sizeof(struct imglink *));

				for (i = 0; i < LINK_PARTS_MAX; i++){
					/* memory allocation for link */
					if (i == 0) {
						ils->lst[ils->num]->link = (char *) malloc(strlen(lps_arr[0])+1 *
						                                           sizeof(char));
						if (&(ils->lst[ils->num]->link) == NULL) {
							fprintf(stderr,
							        "getimglinks_readmanga.c: error allocating ils->lst[%d]->link",
							        ils->num);
						}
					} else {
						ils->lst[ils->num]->link = (char *) realloc(ils->lst[ils->num]->link,
						                                            strlen((ils->lst[ils->num]->link) + 1) +
						                                            strlen(lps_arr[i]) *
						                                            sizeof(char));
						if (ils->lst[ils->num]->link == NULL) {
							fprintf(stderr,
							        "getimglinks_readmanga.c: error allocating ils->lst[%d]->link",
							        ils->num);
						}
					}

					/* imglink generation */
					if (i == 0) {
						strcpy(ils->lst[ils->num]->link, lps_arr[0]);
					} else {
						strcat(ils->lst[ils->num]->link, lps_arr[i]);
					}
				}

				/* cleaning */
				for (i=0; i<LINK_PARTS_MAX; i++) {
					free(lps_arr[i]);
					lps_arr[i] = NULL;
				}
				free(lps_arr);
				lps_arr = NULL;

				lp_num = 0;
				ils->num++;
			}

		char_num++;
		}

	return ils; 
}
